function LP = logp(mdl, X)
%LOGP Evaluates the logarithm of the probability densities on samples
%
% [ Syntax ]
%   - LP = logp(mdl, X)
%
% [ Arguments ]
%   - mdl:          The Gaussian model(s)
%   - X:            The samples on which the pdf is evaluated
%   - LP:           The resultant log-pdf value
%
% [ Description ]
%   - LP = logp(mdl, X) evaluates the pdf of the model on samples given 
%     in X.
%
%     Suppose there are n samples, then X should be a matrix with each
%     column giving a sample. mdl can be single model (K = 1) or an array
%     or K model objects.
%
%     Then in the output, LP is a K x n matrix, with each row for a model,
%     and each column for a sample.
%
% [ History ]
%   - Created by Dahua Lin, on Dec 24, 2007
%

%% parse and verify input

assert(isnumeric(X) && ndims(X) == 2, ...
    'sltoolbox:slcdiaggauss:logp:invalidarg', ...
    'X should be a numeric matrix.');

K = numel(mdl);
d = getdim(mdl(1));

assert(size(X,1) == d, ...
    'sltoolbox:slcdiaggauss:madist:invalidarg', ...
    'The dimension of samples does not match that of the models.');



%% main

% compute M-distances
mds = madist(mdl, X);

% get log(det(cov))
clevel = mdl(1).clevel;
if clevel >= 1
    lds = [mdl.ldsm]';
else
    lds = sum(log([mdl.sigma]), 1)';
end

% calculate the log-pdf
if K == 1
    LP = mds + (d * log(2*pi) + lds);
else
    LP = bsxfun(@plus, mds, (d * log(2*pi) + lds));
end

LP = LP * (-0.5);
